using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using myd3d;
using Microsoft.DirectX;

namespace Gravity
{
	[Serializable]
	public class Trace
	{
		public Vector3 pos;
		public float mass;
	}

	[Serializable]
	public class PSpot
	{
		[NonSerialized]
		public Model model;

		public List<Trace> trace;

		public Vector3 pos;

		public Vector3 heading;
        public float mass;


        [DllImport("cppDll.dll")]
        private static extern void Test(
            float[] values,
            int length,
            int traceCount
        );

        public static void Calc(List<PSpot> spotList, int traceCount)
        {
            float[] values = new float[spotList.Count * 8 + traceCount * spotList.Count * 4];

            for (int i = 0; i < spotList.Count * 8 + traceCount * spotList.Count * 4; i++)
            {
                values[i] = 0.0f;
            }

            for (int i = 0; i < spotList.Count; i++)
            {
                PSpot p = spotList[i];
                values[i * 8 + 0] = p.pos.X; values[i * 8 + 1] = p.pos.Y; values[i * 8 + 2] = p.pos.Z;
                values[i * 8 + 3] = p.heading.X; values[i * 8 * 8 + 4] = p.heading.Y; values[i * 8 + 5] = p.heading.Z;
                values[i * 8 + 6] = p.mass;
                values[i * 8 + 7] = (float)Math.Pow(p.mass, 0.33333);
            }


            Test(values, spotList.Count, traceCount);
            int _length = spotList.Count;
            for (int index = 0; index < traceCount; index++)
            {
                for (int i = 0; i < spotList.Count; i++)
                {
                    PSpot p = spotList[i];
                    Trace t = new Trace();
                    t.pos.X = values[_length * 8 + index * _length * 4 + i * 4 + 0];
                    t.pos.Y = values[_length * 8 + index * _length * 4 + i * 4 + 1];
                    t.pos.Z = values[_length * 8 + index * _length * 4 + i * 4 + 2];
                    t.mass = values[_length * 8 + index * _length * 4 + i * 4 +3];
                    p.trace.Add(t);
                }
            }
        }
        
	}
}
